package com.sundhage.gui
{
	import flash.display.*;
	import flash.events.*;

	public class JSPageList extends Sprite {
		
		public static const NEED_ITEMS:String = "jsplistneeditems";
		public static const ITEM_CLICK:String = "jsplistitemclick";
		
		private var items:Array;
		private var plate:Sprite;
		private var settings:JSPageListSettings;
		private var assets:JSPageListAssets;
		private var bkg:Sprite;
		
		private var onPage:int;
		private var pages:int;
		private var itemsPerPage:int;
		public var totalItems:int;
		private var nextBtn:Sprite;
		private var prevBtn:Sprite;
		private var FFBtn:Sprite;
		private var RWBtn:Sprite;
		public var lastClickedData:Object;
		
		private var txtPosition:JSText;
		
		public var startId:int=0;
		
		public function setOnPage(page:int):void {
			 onPage = page;
			 startId = onPage*itemsPerPage;
		}
		
		public function putOnTop(target:*):void {
			for (var i:int = 0; i<items.length; i++)
				if (target == items[i].content) {
					plate.addChild(items[i]);
				
				}
			
		}
		
		private function getNextY():int {
			if (!items) return 0;
			var yoffs:int = settings.generalPad;
			for (var i:int = 0; i<items.length; i++) {
				if (settings.fixedHeight)
					yoffs += settings.itemHeight+settings.generalPad;
				else
					yoffs += items[i].height+settings.generalPad;
			}
			return yoffs;
		}
		
		private function itemClick(e:Event):void {
			lastClickedData = e.target.data;
			dispatchEvent(new Event(ITEM_CLICK));
//			trace("itemclick!");
		}
		
		public function addItem(content:*, data:Object = null):void {
			//trace("Start: "+new Date().time);
			
			if (!items) items = new Array();
			
			var item:ListItem = new ListItem(content as DisplayObject, assets.getBackground(), data);
			
			
			item.buttonMode = settings.itemsClickable;
			item.addEventListener(ListItem.CLICK, itemClick);
			item.x = settings.generalPad;
			item.y = getNextY();
			
			plate.addChild(item);
			//trace("End: "+new Date().time);
			
			//trace(items.length);
			//items.push("hej");
			items.push(item);
			
		}
		public function clear():void {
			//trace("clear!");
			initPlate();
		}
		
		
		private function initPlate():void {
			try { removeChild(plate); } catch (e:Error) {}
			plate = new Sprite();
			//plate.graphics.beginFill(0xff00ff, 1);
			//plate.graphics.drawRect(0,0,100,100);
			addChild(plate);
			items = new Array();
		}
		
		
		public function requestPage():void {
			startId = onPage*itemsPerPage;
			initPlate();
			updateTxtPosition();
			dispatchEvent(new Event(NEED_ITEMS));
		}
		
		private function updateTxtPosition():void {
			//trace("onpage: "+onPage);
			txtPosition.text = String(onPage+1)+"/"+String(pages);
			txtPosition.x = settings.width/2-txtPosition.width/2;
		}
		
		
		// TODO: Hantera nästa sida/föregående sida...
		
		
		private function nextPage(e:Event):void {
			var lastPage:int = onPage;
			onPage++;
			if(onPage >= pages) onPage = pages-1;
			if (lastPage != onPage) requestPage();
		}
		
		private function prevPage(e:Event):void {
			var lastPage:int = onPage;
			onPage--;
			if (onPage<0) onPage=0;
			if (lastPage != onPage) requestPage();
		}
		
		private function FFPage(e:Event):void {
			var lastPage:int = onPage;
			onPage += settings.FFRWAmount;
			if (onPage >= pages) onPage = pages-1;
			if (lastPage != onPage) requestPage();
		}
		
		private function RWPage(e:Event):void {
			var lastPage:int = onPage;
			onPage -= settings.FFRWAmount;
			if (onPage < 0) onPage = 0;
			if (lastPage != onPage) requestPage();
		}
		
		public function update():void {
			itemsPerPage = settings.itemsPerPage;
			var rest:int = totalItems%itemsPerPage;
			pages = (totalItems-rest)/itemsPerPage;
			//trace("pages: "+pages);
			//trace("onpage: "+onPage);
			if (rest > 0) pages++;
			if (onPage >= pages) onPage = pages-1;
			if (pages>0 && onPage ==-1) onPage = 0;
			updateTxtPosition();
		}
		private function initNav():void {
			totalItems = settings.totalItems;
			itemsPerPage = settings.itemsPerPage;
			var rest:int = totalItems%itemsPerPage;
			pages = (totalItems-rest)/itemsPerPage;
			if (rest > 0) pages++;
				
			var ystart:int = settings.height+settings.itemPad;
			
			txtPosition = new JSText("", JSText.CreateTextFormat(12, settings.font), 0x0, true, false);
			txtPosition.y = ystart;
			updateTxtPosition();
			addChild(txtPosition);
			
			nextBtn = assets.getNextPage();
			prevBtn = assets.getPrevPage();
			nextBtn.buttonMode = true;
			prevBtn.buttonMode = true;
			
			nextBtn.y = prevBtn.y = ystart;
			nextBtn.x = settings.width-nextBtn.width-settings.generalPad;
			prevBtn.x = settings.generalPad;
			nextBtn.addEventListener(MouseEvent.CLICK, nextPage);
			addChild(nextBtn);
			prevBtn.addEventListener(MouseEvent.CLICK, prevPage);
			addChild(prevBtn);
			
			if (settings.hasFFRW) {
				
				FFBtn = assets.getFFPage();
				RWBtn = assets.getRWPage();
				FFBtn.buttonMode = true;
				RWBtn.buttonMode = true;
				FFBtn.y = RWBtn.y = ystart;
				
				FFBtn.x = nextBtn.x-FFBtn.width-settings.itemPad;
				RWBtn.x = prevBtn.x+prevBtn.width+settings.itemPad;
				
				FFBtn.addEventListener(MouseEvent.CLICK, FFPage);
				RWBtn.addEventListener(MouseEvent.CLICK, RWPage);
				
				addChild(FFBtn);
				addChild(RWBtn);
				
			}
			
		}
		
		public function JSPageList(settings:JSPageListSettings, assets:JSPageListAssets, onPage:int=0) {
			// hum: måste skicka lite data fram och tillbaka... 
			// ska veta hur många items som finns, och när en flip sker så ska den dispatcha
			// och BE om nya items.. Annars kommer det inte funka.. Jobbigt!
			this.settings = settings;
			this.assets = assets;
			this.onPage = onPage;
			bkg = new Sprite();
			var bkgContent:Sprite = assets.getBackground();
			bkgContent.width = settings.width;
			bkgContent.height = settings.height;
			bkg.addChild(bkgContent);
			addChild(bkg);
			initPlate();
			initNav();
			
		}
		
	}
}

import flash.display.*;
import flash.events.*;

class ListItem extends Sprite {
	public var content:*;
	public var plate:Sprite;
	public var data:Object;
	public static const CLICK:String = "itemclick";
	private function onClick(e:Event):void {
		dispatchEvent(new Event(CLICK));
	}
	public function ListItem(content:DisplayObject, plate:Sprite, data:Object) {
		this.data = data;
		this.plate = plate;
		this.content = content;
		//trace(content.height);
//		plate.width = content.width;
//		plate.height = content.height;
		plate.addChild(content);
		addChild(plate);
		addEventListener(MouseEvent.CLICK, onClick);
	}
}